<extend name="Layout:layout_content" />
<block name="body_main">
    <div class="grid-parent xsui-page-header">
        <div class="grid-parent">
            <span class="xsui-input-group">
                <form method="get">               
                    开始日期：<input type="text" name="date_start" class="datepicker" size="10" placeholder="开始日期" value="{$search.date_start}" />
                    结束日期：<input type="text" name="date_end" class="datepicker" size="10" placeholder="结束日期" value="{$search.date_end}" />
                    旅行社：<select name="travel_agency_id" data-url="{:U('TravelAgencyConf/get_json_list')}" title="旅行社" data-value="{$search.travel_agency_id}"><option>请选择</option></select>
                    客源类型：<select name="tourist_type_id" data-url="{:U('TouristTypeConf/get_json_list')}" title="客源类型" data-value="{$search.tourist_type_id}"><option>请选择</option></select>
                    讲解厅：<select name="interpretation_room_id" data-url="{:U('InterpretationRoomConf/get_json_list')}" title="讲解厅" data-value="{$search.interpretation_room_id}"><option>请选择</option></select>
                    销售类型：<select name="sale_type" title="销售类型" data-value="{$search.sale_type}"><option>请选择</option><option value="现场">现场</option><option value="定金">定金</option></select>
                    讲解员：<select name="interpreter_id" data-url="{:U('InterpreterConf/get_json_list')}" title="讲解员" data-value="{$search.interpreter_id}"><option>请选择</option></select>
                    导游信息：<select name="guide_id" data-url="{:U('GuideConf/get_json_list')}" title="导游信息" data-value="{$search.guide_id}"><option>请选择</option></select>
                    <input type="text" name="search_key" value="{$search.search_key}" placeholder="车牌/卡号/销售类型" />
                    <button type="submit" class="ui-state-default"><i class="fa fa-search"></i>搜索</button>
                    <input type="hidden" name="store_id" value="{$store_id}" />                 
                </form>
            </span>
        </div>
        <div class="grid-60 mobile-grid-100 grid-parent xsui-hidden">
            <form method="get">
                <div>
                    开始日期：<select name="operate_date_start" required="required">{:sofn_combobox_option(array(''=>'请选择','EQ'=>'等于','GT'=>'大于','LT'=>'小于'),$search['operate_date_start'],'EQ')}</select><input type="text" name="date_start" class="datepicker" size="10" placeholder="开始日期" value="{$search.date_start}" />
                    结束日期：<select name="operate_date_end" required="required">{:sofn_combobox_option(array(''=>'请选择','EQ'=>'等于','GT'=>'大于','LT'=>'小于'),$search['operate_date_end'],'EQ')}</select><input type="text" name="date_end" class="datepicker" size="10" placeholder="结束日期" value="{$search.date_end}" />
                </div>
                <div style="margin:2px 0;">
                    旅行社：<select name="travel_agency_id" data-url="{:U('TravelAgencyConf/get_json_list')}" title="旅行社" data-value="{$search.travel_agency_id}"><option>请选择</option></select>
                    客源类型：<select name="tourist_type_id" data-url="{:U('TouristTypeConf/get_json_list')}" title="客源类型" data-value="{$search.tourist_type_id}"><option>请选择</option></select>
                    讲解厅：<select name="interpretation_room_id" data-url="{:U('InterpretationRoomConf/get_json_list')}" title="讲解厅" data-value="{$search.interpretation_room_id}"><option>请选择</option></select>
                </div>
                <div>
                    销售类型：<select name="sale_type" title="销售类型" data-value="{$search.sale_type}"><option>请选择</option><option value="现场">现场</option><option value="定金">定金</option></select>
                    讲解员：<select name="interpreter_id" data-url="{:U('InterpreterConf/get_json_list')}" title="讲解员" data-value="{$search.interpreter_id}"><option>请选择</option></select>
                    导游信息：<select name="guide_id" data-url="{:U('GuideConf/get_json_list')}" title="导游信息" data-value="{$search.guide_id}"><option>请选择</option></select>
                    <button type="submit" class="ui-state-default"><i class="fa fa-filter"></i>筛选</button>
                </div>
            </form>            
        </div>
        <div class="grid-40 mobile-grid-100 grid-parent xsui-text-right xsui-hidden">
            <a href="#" class="xsui-btn ui-state-default xsui-lnk-refresh"><i class="fa fa-refresh"></i>刷新</a>
            <span class="xsui-input-group">
                <form method="get">
                    <input type="text" name="search_key" value="{$search.search_key}" placeholder="车牌/卡号/销售类型" />
                    <button type="submit" class="ui-state-default"><i class="fa fa-search"></i>搜索</button><input type="hidden" name="store_id" value="{$store_id}" /><!--<button id="btn-search-dialog" class="ui-state-default xsui-icon-only" title="高级搜索"><i class="fa fa-gear"></i></button>-->
                </form>
            </span>
        </div>
        <div id="search-dialog" class="xsui-hidden" style="width:510px;">
            <form method="get" class="xsui-form">
                <table>
                    <tr>
                        <th>开始日期：</th><td><select name="operate_date_start" required="required">{:sofn_combobox_option(array(''=>'请选择','EQ'=>'等于','GT'=>'大于','LT'=>'小于'),$search['operate_date_start'],'EQ')}</select></td><td><input type="text" name="date_start" class="datepicker" size="10" placeholder="开始日期" value="{$search.date_start}" /></td>
                        <th>结束日期：</th><td><select name="operate_date_end" required="required">{:sofn_combobox_option(array(''=>'请选择','EQ'=>'等于','GT'=>'大于','LT'=>'小于'),$search['operate_date_end'],'EQ')}</select></td><td><input type="text" name="date_end" class="datepicker" size="10" placeholder="结束日期" value="{$search.date_end}" /></td>
                    </tr>
                    <tr>
                        <th>旅行社：</th><td colspan="2"><select name="travel_agency_id" data-url="{:U('TravelAgencyConf/get_json_list')}" title="旅行社" data-value="{$search.travel_agency_id}"><option>请选择</option></select></td>
                        <th>车牌：</th><td colspan="2"><input type="text" name="license_plate_number" size="23" placeholder="车牌" value="{$search.license_plate_number}" /></td>
                    </tr>
                    <tr>
                        <th>卡号：</th><td colspan="2"><input type="text" name="card_number" size="23" placeholder="卡号" value="{$search.card_number}" /></td>
                        <th>人数：</th><td><sohtml:select name="operate_person_count" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="person_count" size="10" placeholder="人数" value="{$search.person_count}" /></td>
                    </tr>
                    <tr>
                        <th>客源类型：</th><td colspan="2"><select name="tourist_type_id" data-url="{:U('TouristTypeConf/get_json_list')}" title="客源类型" data-value="{$search.tourist_type_id}"><option>请选择</option></select></td>
                        <th>讲解厅：</th><td colspan="2"><select name="interpretation_room_id" data-url="{:U('InterpretationRoomConf/get_json_list')}" title="讲解厅" data-value="{$search.interpretation_room_id}"><option>请选择</option></select></td>
                    </tr>
                    <tr>
                        <th>丝绸销售：</th><td><sohtml:select name="operate_sichou_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="sichou_amount" size="10" placeholder="丝绸销售" value="{$search.sichou_amount}" /></td>
                    <th>丝巾销售：</th><td><sohtml:select name="operate_sijin_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="sijin_amount" size="10" placeholder="丝巾销售" value="{$search.sijin_amount}" /></td>
                    </tr>
                    <tr>
                        <th>百货销售：</th><td><sohtml:select name="operate_department_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="department_amount" size="10" placeholder="百货销售" value="{$search.department_amount}" /></td>
                    <th>现金金额：</th><td><sohtml:select name="operate_cash_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="cash_amount" size="10" placeholder="现金金额" value="{$search.cash_amount}" /></td>
                    </tr>
                    <tr>
                        <th>刷卡金额：</th><td><sohtml:select name="operate_credit_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="credit_amount" size="10" placeholder="刷卡金额" value="{$search.credit_amount}" /></td>
                    <th>总额：</th><td><sohtml:select name="operate_product_amount" items="EQ:等于;GT:大于;LT:小于" value="EQ" required="required" /></td><td><input type="text" name="product_amount" size="10" placeholder="总额" value="{$search.product_amount}" /></td>
                    </tr>
                    <tr>
                        <th>销售类型：</th><td colspan="2"><select name="sale_type" title="销售类型" data-value="{$search.sale_type}"><option>请选择</option><option value="现场">现场</option><option value="定金">定金</option></select></td>
                        <th>讲解员：</th><td colspan="2"><select name="interpreter_id" data-url="{:U('InterpreterConf/get_json_list')}" title="讲解员" data-value="{$search.interpreter_id}"><option>请选择</option></select></td>
                    </tr>
                    <tr>
                        <th>导游信息：</th><td colspan="5"><select name="guide_id" data-url="{:U('GuideConf/get_json_list')}" title="导游信息" data-value="{$search.guide_id}"><option>请选择</option></select></td>
                    </tr>
                </table>
                <button type="submit">保存</button>
            </form>
        </div>
    </div>
    <div id="grid-container">
        {$content}
    </div>
</block>
<block name="body_after">
    <script type="text/javascript">
        $(function () {
            // 2016-5-26
            ajax_combobox({selector: 'select[name="travel_agency_id"]', def_option: '请选择'});
            ajax_combobox({selector: 'select[name="tourist_type_id"]', def_option: '请选择'});
            ajax_combobox({selector: 'select[name="interpretation_room_id"]', def_option: '请选择'});
            ajax_combobox({selector: 'select[name="interpreter_id"]', def_option: '请选择'});
            ajax_combobox({selector: 'select[name="guide_id"]', def_option: '请选择'});
            // 编辑表单 2016-5-27
            $('.xsui-lnk-edit').click(function () {
                var $form = $('#form');
                $.get($(this).data('url'), function (data) {
                    for (var attr in data) {
                        $form.find('[name="' + attr + '"]').val(data[attr]);
                    }
                })
                return false;
            })
            // 提交表单 2016-5-26
            $('#lnk-save').click(function () {
                var $form = $('#form');
                // 表单验证
                var el, err = "", label;
                $form.find('input,select').each(function () {
                    el = $(this);
                    if (el.prop('required')) {
                        if (!el.val()) {
                            //console.log(el.val());
                            label = el[0].tagName == "INPUT" ? el.prop('placeholder') : el.attr('title');
                            err += (label ? label : el.attr('name')) + "无效!\n";
                        }
                    }
                })
                if (err) {
                    alert(err);
                    return false;
                }
                // 金额验证
                if (!check_amount()) {
                    return false;
                }
                // 提交表单
                var param = $form.find('input, select').serialize();
                $.post($form.data('action'), param, function (result) {
                    //console.log(result);
                    if (result.status) {
                        alert('保存成功!');
                        location.reload();
                    } else {
                        alert(result.info);
                    }
                })
                return false;
            })
            // 金额验证 2016-5-27
            /*
             sichou_amount,sijin_amount,department_amount
             cash_amount,credit_amount
             field_amount,deposit_amount
             product_amount
             */
            function check_amount() {
                var amounts = {
                    sichou_amount: parseInt($('input[name="sichou_amount"]').val()),
                    sijin_amount: parseInt($('input[name="sijin_amount"]').val()),
                    department_amount: parseInt($('input[name="department_amount"]').val()),
                    cash_amount: parseInt($('input[name="cash_amount"]').val()),
                    credit_amount: parseInt($('input[name="credit_amount"]').val()),
                    product_amount: parseInt($('input[name="product_amount"]').val()),
                };
                // 检测空值
                var label, err = "";
                for (var attr in amounts) {
                    if (!amounts[attr]) {
                        label = $('input[name="' + attr + '"]').prop('placeholder');
                        err += label + "不能为空!\n";
                    }
                }
                if (err) {
                    alert(err);
                    return false;
                }
                var v1 = amounts.sichou_amount + amounts.sijin_amount + amounts.department_amount;
                var v2 = amounts.cash_amount + amounts.credit_amount;
                if (v1 != amounts.product_amount) {
                    alert('(丝绸销售+丝巾销售+百货销售)与(总额)不一致!');
                    return false;
                }
                if (v2 != amounts.product_amount) {
                    alert('(现金金额+刷卡金额)与(总额)不一致!');
                    return false;
                }
                return true;
            }
            // 自动计算 2016-5-27
            $('input[name="sichou_amount"],input[name="sijin_amount"],input[name="department_amount"]').blur(function () {
                calc_amount();
            })
            // 自动计算 2016-5-27
            function calc_amount() {
                var amounts = {
                    sichou_amount: parseInt($('input[name="sichou_amount"]').val()),
                    sijin_amount: parseInt($('input[name="sijin_amount"]').val()),
                    department_amount: parseInt($('input[name="department_amount"]').val()),
                };
                var err = false;
                for (var attr in amounts) {
                    if (!amounts[attr]) {
                        err = true;
                        break;
                    }
                }
                if (err) {
                    return;
                }
                $('input[name="product_amount"]').val((amounts.sichou_amount + amounts.sijin_amount + amounts.department_amount));
            }
        })
    </script>
</block>